/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webshop.web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpSession;

import webshop.data.ShoppingBasket;
import webshop.data.OrderInvoice;
import webshop.data.Person;

/**
 * Class that takes a shoppingbasket and regesters it as a completetd order (orderInvoice).
 * @author Ohm
 */
public class PlaceOrderServlet extends AbstractSuperServlet {
    @Resource
    UserTransaction tx;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        EntityManager em = emf.createEntityManager();

        // Variables.
        ShoppingBasket orderBasket;
        HttpSession session = request.getSession();
        
         try {
             // Start transaction connection.
            tx.begin();

            // checks if the person is logged in.
            PersonUtil personUtil= new PersonUtil(em, request);
            if(personUtil.isActive() == null){
                out.println("Please Login!");
            } else{
            Person person = personUtil.isActive();

            // finds the belonging basket.
            orderBasket = personUtil.getShoppingBasket(person);
            //Checks if its empty.
            if(orderBasket.getProductList().size() == 0){
                out.println("You have no products in you basket!");
                out.println("<a href=customerPage.html>Go back</a>");
            } else{
                // Adding the basket to orderinvoices.
                OrderInvoice ordInv = new OrderInvoice(orderBasket);
                em.persist(ordInv);
                if(personUtil.createShoppingBasket(person) != null){
                    // Added a new Active shopping basket to the Person.
                    out.println("<b>Added a new shopping basket to the Person.</b><br/>");
                    out.println("All items in your shoppingbasket has been ordered<br/>");
                    out.println("Thank you come again<br/>");
   
                if(personUtil.isActive() != null)
                    session.invalidate(); // fix to refresh the session object
                    Person p = personUtil.findPerson(person.getName()); // get a updated person object
                    session = request.getSession(); // And create a new.
                    session.setAttribute(person.getName(), p); // Update the session with the object.
                }
            }
            out.println("<meta http-equiv=Refresh content=3;url=customerPage.html> ");
            out.println("<br/>");
            out.println("Redirect in 3 sec.");
            // commit changes in database.
            tx.commit();
            }
        }catch(Exception e) {
            out.println("<pre>");
            e.printStackTrace(out);
            out.println("</pre>");
        } finally { 
            out.close();
        }
    }
}

